<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>背景切换</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            background: url('1.jpg') no-repeat;
            background-size: 100%;
        }
        div{
            width: 100%;
            height: 120px; 
            
        }
        img{
            width: 192px;
            display: none;
            transition: display 3s linear;
            cursor: pointer;
        }
        ul{
            list-style: none;
        }
        ul li{
            float: left;
            margin-left: 70px;
        }
        div:hover img{
            display: inline-block;
        }

    </style>
</head>
<body>
    <div>
        <ul>
            <li><img src="1.jpg" alt="" class="bg_img"></li>
            <li><img src="2.jpg" alt="" class="bg_img"></li>
            <li><img src="3.jpg" alt="" class="bg_img"></li>
            <li><img src="4.jpg" alt="" class="bg_img"></li>
        </ul>
    </div>
    <script>
        bg_obj = document.getElementsByTagName('body')[0];
        li_obj = document.getElementsByClassName('bg_img');
        console.log(li_obj.length)
        for(var i=0;i<li_obj.length;i++){
            var each_li = li_obj[i];
            each_li.index = i+1;
            li_obj[i].onclick = function(){
                bg_obj.style.background = `url('${this.index}.jpg') no-repeat`
            }
        }
    </script>
</body>
</html>